<?php

namespace App\Http\Controllers;

use App\Exceptions\GiftLessonShareNotFoundException;
use App\Models\GiftLessonAcceptance;
use App\Models\GiftLessonShare;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class GiftLessonShareController extends BaseController
{
    //

    public function retrieve(Request $request)
    {
        $this->validate($request, [
            'code' => 'string'
        ]);

        $code = $request->input('code');
        $studentId = intval($request->header("X-Authenticated-Userid"));
        if (!$code) {
            $share = GiftLessonShare::where('student_id', $studentId)
                ->orderBy('id', 'desc')
                ->first();
        } else {
            $share = GiftLessonShare::where('share_code', $code)->first();
        }

        if (empty($share) || $share->getStudentId() !== $studentId) {
            throw new GiftLessonShareNotFoundException();
        }

        $status = $share->getStatus();
        /**
         * @var GiftLessonAcceptance $acceptance
         */
        $acceptance = $share->acceptances()->first();
        if ($status == GiftLessonShare::STATUS_CREATED && Carbon::now()->greaterThan($share->getExpiredAt())) {
            $status = GiftLessonShare::STATUS_ACCEPTED_EXPIRED;
        } else if ($status == GiftLessonShare::STATUS_ACCEPTED) {
            if (!empty($acceptance)) {
                if ($acceptance->getStatus() == GiftLessonAcceptance::STATUS_ACCEPTED && Carbon::now()->greaterThan($acceptance->getExpiredAt())) {
                    $status = GiftLessonShare::STATUS_GIFT_EXPIRED;
                    $acceptance->setStatus(GiftLessonAcceptance::STATUS_EXPIRED);
                    $acceptance->save();
                }
            }
        }

        if ($status != $share->getStatus()) {
            $share->setStatus($status);
            $share->save();
        }

        return new JsonResponse([
            'code' => $share->getShareCode(),
            'lessonCount' => $share->getLessonCount(),
            'expiredAt' => $share->getExpiredAt()->getTimestamp(),
            'status' => $status,
            'acceptance' => empty($acceptance) ? null : [
                'studentId' => $acceptance->getStudentId(),
                'status' => $acceptance->getStatus(),
                'expiredAt' => $acceptance->getExpiredAt()->getTimestamp(),
                'giftedAt' => empty($acceptance->getGiftedAt()) ? null : $acceptance->getGiftedAt()->getTimestamp()
            ]
        ]);

    }

    public function retrieveForPublic(Request $request)
    {
        $this->validate($request, [
            'code' => 'required|string'
        ]);
        $share = GiftLessonShare::where('share_code', $request->input('code'))->first();
        if (empty($share)) {
            throw new  GiftLessonShareNotFoundException();
        }

        if ($share->getStatus() == GiftLessonShare::STATUS_CREATED && Carbon::now()->greaterThan($share->getExpiredAt())) {
            $share->setStatus(GiftLessonShare::STATUS_ACCEPTED_EXPIRED);
            $share->save();
        }

        return new JsonResponse([
            'studentId' => $share->getStudentId(),
            'code' => $share->getShareCode(),
            'status' => $share->getStatus(),
            'expiredAt' => $share->getExpiredAt()->getTimestamp(),
        ]);

    }
}
